//
//  BN_NoticeViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-3-28.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_NoticeViewController.h"

@interface BN_NoticeViewController () <SRRefreshDelegate> {
    BN_NoticeViewCtrlData *noticeData;
    MJRefreshHeaderView *_header;
    SRRefreshView   *_slimeView;
}
@end

@implementation BN_NoticeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 显示Tabbar
    self.tabBarController.tabBar.hidden = NO;
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    self.navigationController.navigationBarHidden = NO;
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:1 forKey:USERDEFAULTS_DYNAMICORNOTICE];
    [userDefaults setInteger:10 forKey:USERDEFAULTS_CLASSPARENT];
    [userDefaults setInteger:10 forKey:USERDEFAULTS_KINDERSECTION];
    [userDefaults synchronize];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self noticeNavgationBar];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushPinLunVC:) name:@"KNOTICE_PUSHPINLUNVC" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noticeModelRefresh:) name:KREFRESH_NOTICELIST object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openReportVc:) name:K_REPORT_NOTICE_INFO object:nil];

    [self joiningTogetherUI];
}

- (void)joiningTogetherUI {
    // __weak typeof(self) weakSelf = self;
    __block BN_NoticeViewController *weakSelf = self;
    // data
    noticeData = [[BN_NoticeViewCtrlData alloc]init];
    [noticeData toDynamic_NoticeReloadList:^(BOOL isReload,BOOL isScrollToTop) {
        if (isReload && isScrollToTop) {
            [weakSelf cancelHintView];
            [weakSelf.noticeTableView reloadData];
        }else {
            [weakSelf cancelHintView];
            [weakSelf.noticeTableView reloadData];
        }
    }];
    [noticeData tableViewRowForCellReload:^(NSArray *indexArr, BOOL isAll) {
        if (isAll) {
            [weakSelf.noticeTableView reloadData];
        }else {
            [weakSelf.noticeTableView reloadRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationNone];
        }
    }];
    
    // tableView
    _noticeTableView = [[UITableView alloc]init];
    [_noticeTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    switch (MAIN_VERSION) {
        case 6:
            _noticeTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            _noticeTableView.contentInset = UIEdgeInsetsMake(0, 0, HEIGHT_TAB_BAR, 0);
            _noticeTableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, HEIGHT_TAB_BAR, 0);
            break;
        default:
            _noticeTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            _noticeTableView.contentInset = UIEdgeInsetsMake(64, 0, HEIGHT_TAB_BAR, 0);
            _noticeTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, HEIGHT_TAB_BAR, 0);
            break;
    }
    [_noticeTableView setDecelerationRate:0.1];
    _noticeTableView.dataSource = noticeData;
    _noticeTableView.delegate = noticeData;
    _noticeTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [self setExtraCellLineHidden:_noticeTableView];
    [self.view addSubview:_noticeTableView];
    
    // 水滴刷新
    // [self initSlimeRefresh];
    
    // 下拉刷新
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.noticeTableView;
    header.delegate = noticeData;
    _header = header;
    
    // 提示
    [self viewLoadHintView];
}

/*
- (void)initSlimeRefresh {
    _slimeView = [[SRRefreshView alloc] init];
    _slimeView.delegate = noticeData;
    noticeData.refreshView = _slimeView;
    if (MAIN_VERSION > 6) {
        _slimeView.upInset = 74;
    }else {
        _slimeView.upInset = 0;
    }
    _slimeView.slimeMissWhenGoingBack = YES;
    _slimeView.slime.bodyColor = APP_OBJECT_REFRESH_BODY;
    _slimeView.slime.skinColor = [UIColor whiteColor];
    _slimeView.slime.lineWith = 0;
    _slimeView.slime.shadowBlur = 4;
    _slimeView.slime.shadowColor = [UIColor blackColor];
    [self.noticeTableView addSubview:_slimeView];
}*/

#pragma mark
#pragma mark Action

- (void)openReportVc:(NSNotification *)notification {
    BN_ReportViewController *reportVc = [[BN_ReportViewController alloc]init];
    [self.navigationController pushViewController:reportVc animated:YES];
}

- (void)noticeModelRefresh:(NSNotification *)notification {
    [noticeData noticeRefresh];
}

- (void)pushPinLunVC:(NSNotification *)notification {
    BN_PingLunViewController *pinglunVC= [[BN_PingLunViewController alloc]init];
    pinglunVC.postId = notification.object;
    [self.navigationController pushViewController:pinglunVC animated:YES];}

- (void)goCreateVC {
    BN_CreateContentViewController *createVC = [[BN_CreateContentViewController alloc]init];
    [createVC toHintMessage:^(BOOL isSuccess) {
        if (isSuccess) {
            [LZCHINT title:@"上传成功" type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
        }
    }];
    [self.navigationController pushViewController:createVC animated:YES];
}

- (void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];
    [tableView setTableFooterView:view];
}

- (void)dealloc
{
    NSLog(@"通知视图控制器释放了...");
}

@end
